package nku.csc360;

/*Raj Kiran Akula
 *CSC 360
 *Homework 2
 *09/23/2009
 */

public class Stock {
	private String symbol;
	private String name;
	private double previousClosingPrice;	
	private double currentPrice;
	private int sharesTraded;
	
	public Stock(String symbol, String name, double previousClosingPrice) {
		this.symbol = symbol;	
		this.name = name;
		this.previousClosingPrice = previousClosingPrice;
		currentPrice = previousClosingPrice;
		sharesTraded = 0;
	}
	
	public String getSymbol() {
		return symbol;	
	}

	public void setSymbol(String symbol) {
		this.symbol = symbol;	
	}

	public String getName() {
		return name;	
	}

	public void setName(String name) {
		this.name = name;	
	}

	public double getPreviousClosingPrice() {
		return previousClosingPrice;	
	}

	public void setPreviousClosingPrice(double previousClosingPrice) {
		this.previousClosingPrice = previousClosingPrice;	
	}

	public double getCurrentPrice() {
		return currentPrice;	
	}

	// current price is set only by the means of transactions

	public int getSharesTraded() {
		return sharesTraded;	
	}

	// shares traded are set only by the means of transactions
	
	public void transaction(int sharesTraded, double price)	{
		this.sharesTraded += sharesTraded;
		currentPrice = price;
	}

	public double changePercent() {
		if (previousClosingPrice == 0.0)
			return 0.0;
		else
			return (currentPrice - previousClosingPrice) / previousClosingPrice * 100.0;	
	}
	
	public static String headerString()	{
		return String.format("Symbol %-24s #Traded  Previous  Current   Chng Pct", "      Name");
	}
	
	//Only this method that is inherited from object is changed!!!!!!!
	
	public String toString() {
		
		//Checked if the previous closing price is a doubleNan and if it is
		//Changed the format to print like required!!!!!!!!
		if(Double.isNaN(previousClosingPrice)){
			return String.format(" %-6s %-24s %6s $%7s $%7.2f %10s",
					symbol, name, String.valueOf(sharesTraded), "---", 
					currentPrice, "---");
		}
		return String.format(" %-6s %-24s %6s $%7.2f $%7.2f %5s%5.2f%%",
		symbol, name, String.valueOf(sharesTraded), previousClosingPrice, 
		currentPrice, (changePercent()>=0.0)?"+":"-", Math.abs(changePercent()));
	}
	
}
